﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KTOP.Homeworks.Hw5.Logics.AlphaMagic
{
    internal class AlphaMatrixBuilder
    {
        private static int GetDifference(IEnumerable<int> psi1, IEnumerable<int> psi2)
        {
            return psi1.Count() + psi2.Count() - psi1.Intersect(psi2).Count();
        }

        public static int[][] Build(Psi.Psi psi)
        {
            return psi.PsiValues.Select((psi1, id) => Enumerable.Repeat(0, id + 1)
                                                          .Concat(psi.PsiValues.Skip(id + 1)
                                                                      .Select(psi2 => GetDifference(psi1, psi2)))
                                                          .ToArray())
                .ToArray();
        }

        public class InnerOuter
        {
            public IEnumerable<int> Inner { get; set; }
            public IEnumerable<int> Outer { get; set; }
        }


        public static InnerOuter ReducePsi(int[][] alpha, Psi.Psi psi)
        {
            var max = alpha.Max(i => i.Max());

            int outer = 0, inner = 0;

            for (var row = 0; row < alpha.Length; row++)
                for (var col = 0; col < alpha.Length; col++)
                    if (alpha[row][col] == max)
                    {
                        outer = row;
                        inner = col;

                        break;
                    }

            var outerPsi = psi.PsiValues.Skip(outer).First().ToList();
            var innerPsi = psi.PsiValues.Skip(inner).First().ToList();

            psi.SetValues(psi.PsiValues.Select(p => p.Except(outerPsi).Except(innerPsi)));

            return new InnerOuter {Inner = innerPsi, Outer = outerPsi};
        }
    }

}